package cn.xiaomubiao.oschine.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.contants.Host;
import cn.xiaomubiao.oschine.util.SpUtils;
import cn.xiaomubiao.oschine.util.ToastUtils;

/**
 * Created by hasee on 2017/4/7.
 */

public class IpSettingFragment extends Fragment {
    @BindView(R.id.edt_ip)
    EditText mEdtIp;
    @BindView(R.id.edt_port)
    EditText mEdtPort;
    @BindView(R.id.btn_save)
    Button mBtnSave;
    @BindView(R.id.btn_clean)
    Button mBtnClean;

    @Nullable
    @Override
    public View onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ) {
        View view = View.inflate(getContext(), R.layout.view_replace_ip_fragment, null);
        ButterKnife.bind(this, view);
        mEdtIp.setText("10.0.2.2");
        mEdtPort.setText("8080");
        return view;
    }


    @OnClick({R.id.btn_save, R.id.btn_clean})
    public void onClick( View view ) {
        switch (view.getId()) {
            case R.id.btn_save:
                String ip = mEdtIp.getText().toString().trim();
                String port = mEdtPort.getText().toString().trim();

                if(!TextUtils.isEmpty(ip) && !TextUtils.isEmpty(port)) {

                    // 保存缓存
                    SpUtils.putString(getContext(),"ip",ip);
                    SpUtils.putString(getContext(),"port",port);

                    Host.HOST_DEFAULT = "http://"+ip+":"+port+"/oschina/";
                    ToastUtils.showToastShort(getContext(),"保存成功");

                    getActivity().finish();

                } else {
                    ToastUtils.showToastShort(getContext(),"ip或端口为空");

                }

                break;

            case R.id.btn_clean:
                mEdtIp.setText("");
                mEdtPort.setText("");
                Host.HOST_DEFAULT = "http://10.0.2.2:8080/oschina/";
                break;

        }
    }
}
